<template>
  <div class="home-view">
    <CemeteryBackground />

    <div class="app-content">
      <AppHeader :active-view="currentView" @view-change="handleViewChange" />

      <main class="main-content">
        <SearchBar />

        <div class="content-view">
          <TimelineView v-if="currentView === 'timeline'" />
          <StatsPanel v-else-if="currentView === 'stats'" />
          <div v-else class="sites-grid">
            <SiteCard
              v-for="site in filteredSites"
              :key="site.name"
              :site="site"
              @click="() => handleSiteClick(site)"
            />
          </div>
        </div>

        <div v-if="filteredSites.length === 0" class="empty-state">
          <div class="empty-icon">🔍</div>
          <h3 class="empty-title">未找到相关网站</h3>
          <p class="empty-desc">
            尝试调整搜索条件或筛选器，或者
            <button class="reset-btn" @click="store.resetFilters">
              重置所有筛选
            </button>
          </p>
        </div>
      </main>

      <AppFooter />
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import { useCemeteryStore } from "@/stores/cemetery";
import AppHeader from "@/components/AppHeader.vue";
import AppFooter from "@/components/AppFooter.vue";
import SearchBar from "@/components/SearchBar.vue";
import StatsPanel from "@/components/StatsPanel.vue";
import TimelineView from "@/components/TimelineView.vue";
import SiteCard from "@/components/SiteCard.vue";
import CemeteryBackground from "@/components/CemeteryBackground.vue";

const store = useCemeteryStore();
const currentView = ref("grid"); // 'grid', 'timeline', 'stats'

// 使用store中的过滤逻辑
const filteredSites = computed(() => store.filteredSites);

// 处理视图切换
const handleViewChange = (view) => {
  currentView.value = view;
  // 同步更新 store 中的 viewMode（如果需要）
  if (view === "timeline" || view === "grid") {
    store.viewMode = view;
  }
};

// 处理网站卡片点击
const handleSiteClick = (site) => {
  // 点击之后，显示网站信息（不再跳转到详情页）
  console.log(`点击了网站：${site.name}`);
  // 这里可以添加其他交互，如显示弹窗等
};

// 初始化
onMounted(() => {
  // 数据已经通过JSON文件直接导入，无需异步加载
});
</script>

<style scoped>
.home-view {
  min-height: 100vh;
  position: relative;
}

.app-content {
  position: relative;
  z-index: 1;
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
  min-height: calc(100vh - 200px);
}

.sites-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 1.5rem;
  margin-top: 2rem;
}

.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  margin-top: 2rem;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
  opacity: 0.6;
}

.empty-title {
  font-size: 1.5rem;
  color: var(--text-primary);
  margin-bottom: 1rem;
}

.empty-desc {
  color: var(--text-secondary);
  font-size: 1rem;
  line-height: 1.6;
}

.reset-btn {
  background: var(--accent-color);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s ease;
  margin-left: 0.5rem;
}

.reset-btn:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}




/* 响应式设计 */
@media (max-width: 768px) {
  .main-content {
    padding: 0 1rem;
  }

  .sites-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
}
</style>
